{
    // 基本定义
    let ajax = function (callback) {
        console.log('执行');
        setTimeout(function () {
            callback && callback.call()
        }, 1000);
    };
    ajax(function () {
        console.log('timeout1');
    })
}

{
    let ajax = function () {
        console.log('执行2');
        return new Promise(function (resolve, reject) {
            setTimeout(function () {
                resolve()
            }, 1000);
        })
    };

    ajax().then(function () {
        console.log('promise', 'timeout2');
    })
}

{
    let ajax = function () {
        console.log('执行3');
        return new Promise(function (resolve, reject) {
            setTimeout(function () {
                resolve()
            }, 1000);
        })
    };

    ajax()
        .then(function () {
            return new Promise(function (resolve, reject) {
                setTimeout(function () {
                    resolve()
                }, 2000);
            });
        })
        .then(function () {
            console.log('timeout3');
        })
}

{
    let ajax = function (num) {
        console.log('执行4');
        return new Promise(function (resolve, reject) {
            if (num > 5) {
                resolve()
            } else {
                reject("error, 进入 reject 啦")
                // throw new Error('出错了')
            }
        })
    }

    ajax(6).then(function () {
        console.log('log', 6);
    }).catch(function (err) {
        console.log('catch', err);
    });

    ajax(3).then(function () {
        console.log('log', 3);
    }).catch(function (err) {
        console.log('catch', err);
    });
}

{
    // promise 实用场景: 所以图片加载完了加载页面
    function loadImg(src){
        return new Promise(function(resolve,reject){
            let img=document.createElement('img')
            img.src = src;
            img.onload =function(){
                resolve(img)
            }
            img.onerror=function(err){
                reject(`${err} error`)
            }
        })
    }

    // 错误案例, 显示错误的内容
    Promise.all([
        loadImg('https://user-gold-cdn.xitu.io/2019/2/12/168df9ec6bf3b894?imageView2/0/w/1280/h/960/format/webp/ignore-error/1'),
        loadImg('https://img.vphotos.cn/FD2BE2FA38E4CB90827925B250094702/postp/logosmall/vbox7082__MGL8062_213106_small.JPG'),
        loadImg('https://img.vphotos.cn/FD2BE2FA38E4CB90827925B250094702/po/logosmall/vbox7082__MGL8062_213106_big.JPG')])
    .then(function(){
        console.log('all img over')
    })
    .catch(function(err){
        console.log(err)
    })

    // 全部成功显示
    Promise.all([
        loadImg('https://user-gold-cdn.xitu.io/2019/2/12/168df9ec6bf3b894?imageView2/0/w/1280/h/960/format/webp/ignore-error/1'),
        loadImg('https://img.vphotos.cn/FD2BE2FA38E4CB90827925B250094702/postp/logosmall/vbox7082__MGL8062_213106_small.JPG')])
    .then(function(){
        console.log('all img over')
    })
    .catch(function(err){
        console.log(err)
    })

    // 有一个成功就行
    Promise.race([
        loadImg('https://user-gold-cdn.xitu.io/2019/2/12/168df9ec6bf3b894?imageView2/0/w/1280/h/960/format/webp/ignore-error/1'),
        loadImg('https://img.vphotos.cn/FD2BE2FA38E4CB90827925B250094702/postp/logosmall/vbox7082__MGL8062_213106_small.JPG'),
        loadImg('https://img.vphotos.cn/FD2BE2FA38E4CB90827925B250094702/po/logosmall/vbox7082__MGL8062_213106_big.JPG')])
    .then(function(){
        console.log('===== one or much img over')
    })
    .catch(function(err){
        console.log(err)
    })


}
